<?php  
class ControllerShoutboxViewMessage extends Controller {
	public function index() { 
    	$this->language->load('module/article_shoutbox');		
		$this->load->model('news/shoutbox');
		$this->data['text_no_messages'] = $this->language->get('text_no_messages');	
		$this->data['text_said'] = $this->language->get('text_said');		
		$this->data['reviews'] = array();		
		$review_total = $this->model_news_shoutbox->getTotalMessages();
			
		$results = $this->model_news_shoutbox->getMessages(0, 10);
      	$this->data['messages']= array();
		foreach ($results as $result) {
        	$this->data['messages'][] = array(
        		'author'     => $result['author'],
				'message'       => $result['message'],
        		'reviews'    => sprintf($this->language->get('text_reviews'), (int)$review_total),
        		'date_added' => date('d/m h:i:s', strtotime($result['date_added']))
        	);
      	}			
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/view_messages.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/news//view_messages.tpl';
		} else {
			$this->template = 'default/template/news//view_messages.tpl';
		}
		
		$this->response->setOutput($this->render());
	}
	
	public function write() {
	
	$this->load->model('news/utf8');
		$this->language->load('module/article_shoutbox');
		
		$this->load->model('news/shoutbox');
		
		$json = array();
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if (($this->model_news_utf8->utf8_strlen($this->request->post['name']) < 2) || ($this->model_news_utf8->utf8_strlen($this->request->post['name']) > 25)) {
				$json['error'] = $this->language->get('error_name');
			}
			
			if (($this->model_news_utf8->utf8_strlen($this->request->post['message']) < 5) || ($this->model_news_utf8->utf8_strlen($this->request->post['message']) > 255)) {
				$json['error'] = $this->language->get('error_message');
			}
			if($this->config->get('article_shoutbox_captcha')==1){
					if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
						$json['error'] = $this->language->get('error_captcha');
					}
			}
			if (!isset($json['error'])) {
				$this->model_news_shoutbox->addMessage($this->request->post);				
				$json['success'] = $this->language->get('text_success');
			}$this->response->setOutput(json_encode($json));
		}
		
		
	}
	
	public function clear_message(){/*User*/ 
		$this->load->library('user');		
	$this->load->model('news/utf8');
		$this->user = new User($this->registry);
		if ($this->user->isLogged()) {
			$this->language->load('module/article_shoutbox');		
			$this->load->model('news/shoutbox');		
			$json = array();
			
			if ($this->request->server['REQUEST_METHOD'] == 'POST') {
				if (($this->model_news_utf8->utf8_strlen($this->request->post['name']) < 2) || ($this->model_news_utf8->utf8_strlen($this->request->post['name']) > 25)) {
					$json['error'] = $this->language->get('error_name');
				}
				
				if (($this->model_news_utf8->utf8_strlen($this->request->post['message']) < 5) || ($this->model_news_utf8->utf8_strlen($this->request->post['message']) > 255)) {
					$json['error'] = $this->language->get('error_message');
				}
				if (!isset($json['error'])) {
					$this->model_news_shoutbox->clear_message();
					$this->model_news_shoutbox->addMessage($this->request->post);				
					$json['success'] = $this->language->get('text_success');
				}
			}
			
			$this->response->setOutput(json_encode($json));
		}else{
			echo "You don't have permission to delete message";
			}
	}
	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
}
?>